<?php
//$Id: stat.php 9 2009-04-26 06:35:12Z bole.Chen $
uses('controller');

class statController extends Controller {
    var $chart;

    function __construct() {
        parent::__construct();
    }

    //销量排行
    function top10() {
        $time = $this->request['time'];

        //默认为本月数据
        if (!$time) $time = date('Y-m-').'1';
        $data['updated'] = strtotime($time);
        $data['flush']   = $this->request['flush'];

        if ($this->request['issale']) $data['where'] = 'b.issale=1 and b.stocknum>0';

        $data['topsale'] = $this->model->topSale($data);
        $data['topin'] = $this->model->topIn($data);
        $data['topin2'] = $this->model->topIn2($data);

        return $data;
    }

    function showMonthIndentCount() {
        $tmp = $this->_dataMonthIndentCount(true);
        $data['data1'] = $tmp[0]['result'];
        $data['data2'] = $tmp[1]['result'];
        return $data;
    }

    function showMonthIncome() {
        $tmp = $this->_dataMonthIncome(true);
        $data['data2'] = $tmp[0]['result'];
        $data['data1'] = $tmp[1]['result'];
        return $data;
    }

    //初始化 ofc
    function ofcinit() {
        uses('ofc');
        $this->chart = new ofc();

        $this->chart->set_bg_colour('#FFFFFF');
    }

    function title($title) {
        if (!$this->chart) $this->ofcinit();

        $title = new title($title);
        $title->set_style( "{font-size: 22px; color: #666666; text-align: center;}" );
        $this->chart->set_title($title);
    }

    function x_label($data, $step=2) {
        if (!$this->chart) $this->ofcinit();

        $x_labels = new x_axis_labels();
        $x_labels->set_steps($step);
        $x_labels->set_vertical();
        $x_labels->set_colour('#666666');
        $x_labels->set_labels($data);
        $x_labels->set_size(14);

        $x = new x_axis();
        $x->set_labels($x_labels);
        $this->chart->set_x_axis($x);
    }

    function y_label($max, $step=100) {
        if (!$this->chart) $this->ofcinit();

        $y = new y_axis();
        $y->set_range(0, $max, $step);
        $this->chart->set_y_axis($y);
    }

    function x_legend($title) {
        if (!$this->chart) $this->ofcinit();

        $x_legend = new x_legend($title);
        $x_legend->set_style( '{font-size: 16px; color: #778877}' );
        $this->chart->set_x_legend( $x_legend );
    }

    function draw() {
        exit($this->chart->toPrettyString());
    }

    //月订单数据
    function _dataMonthIndentCount($return=false) {
        $tmp = $this->queryData("SELECT date_format( FROM_UNIXTIME(created) , '%Y-%m' ) as dt , count(*) as ct
            FROM `indent` WHERE stat>1
            GROUP BY date_format( FROM_UNIXTIME(created) , '%Y%m' )");

        $tmp2 = $this->queryData("SELECT date_format( FROM_UNIXTIME(created) , '%Y-%m' ) as dt , count(*) as ct
            FROM `indent`
            GROUP BY date_format( FROM_UNIXTIME(created) , '%Y%m' )");

        if ($return) return array($tmp, $tmp2);

        foreach ($tmp['result'] as $v) {
            $data1[] = (int)$v['ct'];
            $months[] = $v['dt'];
        }

        foreach ($tmp2['result'] as $v) {
            $data2[] = (int)$v['ct'];
        }

        $this->title('订单数统计');

        $line_dot = new line_dot();
        $line_dot->set_width(3);
        $line_dot->set_colour('#339933');
        $line_dot->set_dot_size(4);
        $line_dot->set_values($data1);
        $line_dot->set_key("有效订单", 10);
        $this->chart->add_element($line_dot);

        $line_dot2 = new line_hollow();
        $line_dot2->set_width(2);
        $line_dot2->set_colour('#999999');
        $line_dot2->set_dot_size(3);
        $line_dot2->set_values($data2);
        $line_dot2->set_key("全部订单", 10);
        $this->chart->add_element($line_dot2);

        $this->x_label($months);
        $this->y_label(max($data2)*1.1, 10);

        $this->x_legend('月份 ('.min($months).' - '.max($months).')');

        $this->draw();
    }

    //营业额统计
    function _dataMonthIncome($return=false) {
        $tmp = $this->queryData("SELECT date_format( FROM_UNIXTIME(updated) , '%Y-%m' ) as dt , SUM(price*amount) as ct
            FROM `indentdetail`
            GROUP BY date_format( FROM_UNIXTIME(updated) , '%Y%m' )");

        $tmp2 = $this->queryData("SELECT date_format( FROM_UNIXTIME(updated) , '%Y-%m' ) as dt , SUM((price-cost)*amount) as ct
            FROM `indentdetail`
            GROUP BY date_format( FROM_UNIXTIME(updated) , '%Y%m' )");

        if ($return) return array($tmp, $tmp2);

        foreach ($tmp['result'] as $v) {
            $data1[] = (int)$v['ct'];
            $months[] = $v['dt'];
        }

        foreach ($tmp2['result'] as $v) {
            $data2[] = (int)$v['ct'];
        }

        $this->title('营业额统计');

        $line_dot = new line_hollow();
        $line_dot->set_width(2);
        $line_dot->set_colour('#666666');
        $line_dot->set_dot_size(3);
        $line_dot->set_values($data1);
        $line_dot->set_key("营业金额", 10);
        $this->chart->add_element($line_dot);

        $line_dot2 = new line_dot();
        $line_dot2->set_width(3);
        $line_dot2->set_colour('#339933');
        $line_dot2->set_dot_size(4);
        $line_dot2->set_values($data2);
        $line_dot2->set_key("毛利", 10);
        $this->chart->add_element($line_dot2);

        $this->x_label($months);
        $this->y_label(max($data1)*1.1, 1000);

        $this->x_legend('月份 ('.min($months).' - '.max($months).')');

        $this->draw();
    }

    function queryData($sql) {
        return $this->model->queryData($sql);
    }
}
?>
